package com.bestcem.xm.label.grpc.service.param;

import com.alibaba.fastjson.annotation.JSONField;
import com.bestcem.xm.common.core.constant.RegTypes;
import com.bestcem.xm.label.service.dto.LabelValueDTO;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import org.springframework.util.ObjectUtils;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 标签值标识
 * @date 2021-06-09
 */
@ToString
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class LabelValueIdParam {
    /**
     * 标签值标识
     */
    @NotNull(message = "标签值标识不能为空")
    @Pattern(regexp = RegTypes.OBJECT_ID, message = "标签值标识格式错误")
    private String id;

    /**
     * 标签标识
     */
    @JsonProperty("label_id")
    @JSONField(name="label_id")
    @NotNull(message = "标签标识不能为空")
    @Pattern(regexp = RegTypes.OBJECT_ID, message = "标签标识格式错误")
    private String labelId;

    /**
     * 创建标签值标识对象
     *
     * @param id
     * @param labelValue
     * @return
     */
    public static LabelValueIdParam of(String id, LabelValueDTO labelValue) {
        LabelValueIdParam labelValueIdParam = new LabelValueIdParam();
        // 设置标签值标识，标签值标识不可为空
        labelValueIdParam.setId(id);

        // 根据需要填充附加信息，如标签标识、标签组标识、标签名称等
        if (labelValue != null) {
            labelValueIdParam.setLabelId(labelValue.getLabelId());
        }
        return labelValueIdParam;
    }

    @Override
    public int hashCode() {
        return ObjectUtils.nullSafeHashCode(id);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof LabelValueIdParam)) {
            return false;
        }

        LabelValueIdParam labelValueIdParam = (LabelValueIdParam) obj;
        return ObjectUtils.nullSafeEquals(id, labelValueIdParam.getId());
    }
}
